import message from './message';

var http = {}

//let api_root = "http://192.168.1.5:8999/web/index.php/applet/";
//体验版外网地址
let api_root = "https://www.hujingx.com/web/index.php/applet/";

var app_id = "wxedd9501e7e6d262a";

/**
 * 
 * @param {*} url 
 * @param {*} data 
 * @param {*} success 
 * @param {*} complete 
 * @param {*} check_login 
 * @param {*} token 
 * @param {*} is_interceptor   是否启动拦截器
 */

function doRequest(  url , data , success , complete , check_login , token , is_interceptor ){

  console.log(token);
  
  wx.request( {
    url: api_root+url,
    data: data,
    header: {
      "context-type":"application/json",
      "token": token
    },
    success( res ){

      wx.hideNavigationBarLoading();

    
       
        if(res.data.status==99){

          wx.showModal({
            content:  res.data.message,
            confirmText:'立即登录',
            success:function( result ){
              if(result.confirm){
                wx.navigateTo({
                  url: '/pages/market/login/login',
                });
              }else{
                console.log("点击取消")
              }
            }
          })
          return;
        }
      

        if(res.data.status==-1){

          message.show_error( res.data.message);
          /*
          wx.showModal({
            cancelColor: 'cancelColor',
            content: res.data.message
          })
          */
        }else{
          success(res)
        }

      
    }
  })

}


http.doGet=function(url , data , success , complete , check_login , is_interceptor){
  wx.showNavigationBarLoading();

  data = Object.assign({
    app_id:app_id,
    //token: wx.getStorageSync("token")
  } , data)


 //var token = wx.getStorageSync('token');


 
 var token = "";
 wx.getStorage({
   key:'token',
   success:function(result){
     token = result.data;


     console.log("获取token");
     console.log(result);
     console.log(result.data);


     doRequest( url , data , success , complete , check_login , token ,is_interceptor );
     
   },
   fail:function(){

    console.log("token 记录不存在");
    doRequest( url , data , success , complete , check_login , "" , is_interceptor );
   }
 })

/*


 console.log("网络请求token");

 console.log(token)

 wx.showLoading({
   title: '数据加载中',
 });
  wx.request( {
    url: api_root+url,
    data: data,
    header: {
      "content-type":"application/json",
      "Authorization": token
    },
    success( res ){
      wx.hideNavigationBarLoading();

      setTimeout(function(){
        wx.hideLoading();
      } , 1000)


      console.log(res.data.status);
      if(res.data.status==99){


        console.log("ppppp");

        wx.showModal({
          content:  res.data.message,
          confirmText:'立即登录',
          success:function( result ){
            if(result.confirm){
              wx.navigateTo({
                url: '/pages/login/login',
              });
            }else{
              console.log("点击取消")
            }

          }
        })
        return;
      }
      if(res.data.status==-1){
        wx.showModal({
          cancelColor: 'cancelColor',
          content: res.data.message
        })
      }else{
        success(res)
      }
    }
  })

  */
}




function doRequestPost(){

}

http.doPost=function( url , data , success , complete , check_login ){
  
  /*
  data = Object.assign({
    app_id: app_id,
    token: wx.getStorageSync("token")
  }, data)

  */

  

  url+='?app_id='+app_id;

  var token = wx.getStorageSync('token');

  wx.request({
    //url: api_root+url,
    url: api_root+url,
    header:{
      'content-type': 'application/x-www-form-urlencoded',
      "token": token
    },
    method: 'POST',
    data:data,
    success:function( res ){

      console.log(res);
      if(res.data.status==99){

        wx.showModal({
          cancelColor: 'cancelColor',
          content:  res.data.message,
          confirmText:'立即去登录',
          success:function( res ){
            if(res.confirm){
              wx.navigateTo({
                url: '/pages/me/me',
              })
            }

          }
        })
        return;


      }else{
        success(res)

      }
    }
  })
}


http.downloadFile = function( url , cb ){

  var url2 = api_root+url
  wx.downloadFile({
    url:url2,
    success:function(){

    }
  })
}




/**
 * 上传文件网络请求接口
 */


 
function uploadFile( url , tempFilePath ,formData , cb , token ){

  var url2 = api_root+url;
  wx.uploadFile({
    url: url2,
    header:{
      //'content-type': 'multipart/form-data',
      'content-type': 'application/x-www-form-urlencoded',
      "token": token
    },
    filePath: tempFilePath,
    name: 'file',
    formData:formData,
    success( res ){
      cb(res.data);
    }
  })


}

http.doUploadFile = function(  url , tempFilePath ,formData , cb  ){
  var token = null;
  wx.getStorage({
    key:'token',
    success:function(result){
      token = result.data;
 
 
      console.log("获取token");
      console.log(result);
      console.log(result.data);
 
      //doRequest( url , data , success , complete , check_login , token  );


      uploadFile( url , tempFilePath ,formData , cb , token )

      
    },
    fail:function(){
 
     console.log("token 记录不存在");
 
     //doRequest( url , data , success , complete , check_login , ""  );
     uploadFile( url , tempFilePath ,formData , cb , "" );
    }
  })
}



//上传文件
http.uploadFile2 = function( url , tempFilePath ,formData , cb ){
  var url2 = api_root+url;
  wx.uploadFile({
    url: url2,
    filePath: tempFilePath,
    name: 'file',
    formData:formData,
    success( res ){
      cb(res.data);
    }
  })

}


module.exports = http